package com.atguigu.spzx.manager.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manager.constant.CacheConstant;
import com.atguigu.spzx.manager.mapper.SysUserMapper;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Override
    public LoginVo login(LoginDto loginDto) {
        //1.判断验证码
        String redisCode = redisTemplate.opsForValue().get(CacheConstant.USER_LOGIN_VALIDATECODE_PREFIX + loginDto.getCodeKey());
        if(redisCode==null||!redisCode.equals(loginDto.getCaptcha())){
            throw new GuiguException(ResultCodeEnum.VALIDATECODE_ERROR);
        }
        // 验证通过删除redis中的验证码
        redisTemplate.delete(CacheConstant.USER_LOGIN_VALIDATECODE_PREFIX + loginDto.getCodeKey()) ;

        //2.判断用户名是否存在,判断密码是否正确
        SysUser sysUser=sysUserMapper.selectByUsername(loginDto.getUserName());
        if(sysUser==null){
            throw  new GuiguException(ResultCodeEnum.LOGIN_ERROR);
        }
        if (!(DigestUtils.md5DigestAsHex(loginDto.getPassword().getBytes())).equals(sysUser.getPassword())){
            throw  new GuiguException(ResultCodeEnum.LOGIN_ERROR);
        }
        //4.创建token
        String token = UUID.randomUUID().toString().replace("-", "");
        sysUser.setPassword(null);//擦除密码
        redisTemplate.opsForValue().set(CacheConstant.USER_LOGIN_PREFIX + token , JSON.toJSONString(sysUser) , 30 , TimeUnit.MINUTES);
        //5.返回数据
        LoginVo loginVo=new LoginVo();
        loginVo.setToken(token);
        return loginVo;
    }

    /*@Override
    public SysUser getUserInfo(String token) {
        String userJson = redisTemplate.opsForValue().get("user:login:" + token);
        if(userJson==null){
            throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
        }
        return JSON.parseObject(userJson , SysUser.class) ;
    }
*/
    @Override
    public void logout(String token) {
        redisTemplate.delete(CacheConstant.USER_LOGIN_PREFIX+token);
    }



    @Override
    public PageInfo<SysUser> findByPage(SysUserDto sysUserDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysUser> list=sysUserMapper.selectByPage(sysUserDto);
        PageInfo pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void saveUser(SysUser sysUser) {
        sysUserMapper.insertSysUser(sysUser);
    }

    @Override
    public void updateSysUser(SysUser sysUser) {
        sysUserMapper.updateSysUser(sysUser);
    }

    @Override
    public void removeById(Long id) {
        sysUserMapper.deleteById(id);
    }
}
